﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MvcToolbox
{
    public class BaseController : Controller
    {

        public new HttpContextBase HttpContext { get; set; }

        /// <summary>
        /// Default constructor
        /// </summary>
        public BaseController()
            : base()
        {
        }

        /// <summary>
        /// Constructor with a HttpContext
        /// This constructor allow unit test with mock on HttpContext
        /// </summary>
        /// <param name="httpContext">Http Context</param>
        public BaseController(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            HttpContext = httpContext;
        }


        /// <summary>
        /// Called when [action executing].
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (this.HttpContext == null)
            {
                HttpContext = base.HttpContext;
            }
            base.OnActionExecuting(filterContext);
        }


        public ViewResult View<T1, T>(T1 model)
        {
            T newModel = AutoMapper.Mapper.Map(model, Activator.CreateInstance<T>());
            return View(newModel);
        }
        public ViewResult View<T1, T>(IView view, T1 model)
        {
            T newModel = AutoMapper.Mapper.Map(model, Activator.CreateInstance<T>());
            return View(view, newModel);
        }

        public ViewResult View<T1, T>(string viewName, T1 model)
        {
            T newModel = AutoMapper.Mapper.Map(model, Activator.CreateInstance<T>());
            return View(viewName, newModel);
        }

        public ViewResult View<T1, T>(string viewName, string masterName, T1 model)
        {
            T newModel = AutoMapper.Mapper.Map(model, Activator.CreateInstance<T>());
            return View(viewName, masterName, newModel);
        }


        public PartialViewResult PartialView<T1, T>(T1 model)
        {
            T newModel = AutoMapper.Mapper.Map(model, Activator.CreateInstance<T>());
            return PartialView(newModel);
        }

        public PartialViewResult PartialView<T1, T>(string viewName, T1 model)
        {
            T newModel = AutoMapper.Mapper.Map(model, Activator.CreateInstance<T>());
            return PartialView(viewName, newModel);
        }
    }
}
